import 'dart:convert';

import 'package:dio/dio.dart' as Resp;
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:ty_smart_watch/bean/update_userinfo_bean.dart';
import 'package:ty_smart_watch/bean/user_detail_bean.dart';
import 'package:ty_smart_watch/bean/user_info_bean.dart';
import 'package:ty_smart_watch/common/common.dart';
import 'package:ty_smart_watch/common/images_url.dart';
import 'package:ty_smart_watch/utils/Log.dart';
import 'package:ty_smart_watch/utils/http_util.dart';

class UserDetailController extends GetxController {
  @override
  void onInit() {
    super.onInit();
    _getUserInfo();
  }

  ///id:
  ///int SEX = 0;
  ///int AGE = 1;
  ///int HEIGHT = 2;
  ///int WEIGHT = 3;
  var list = <UserDetailBean>[
    UserDetailBean(0, "性别", 0, ""),
    UserDetailBean(1, "年龄", -1, "岁"),
    UserDetailBean(2, "身高", -1, "cm"),
    UserDetailBean(3, "体重", -1, "kg"),
  ].obs;

  var userName = "Lightthings".obs;
  var avatarUrl = SETTING_AVATAR.obs;

  updateData(int id, int data) {
    // Log.d("更新请求：$id >  $data");
    var item = list.firstWhere((element) => element.id == id);
    item.data = data;
    list.refresh();
  }

  updateName(String name) {
    userName(name);
  }

  updateAvatarUrl(String url) {
    avatarUrl(url);
  }

  _getUserInfo() async {
    Log.d("退出登录");
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String token = prefs.getString(SP_ACCESS_TOKEN) ?? "";
    print("token ：$token");
    var dio = HttpUtil.instance?.serviceDio();
    dio.options.headers['tenant-id'] = "1";
    dio.options.headers['Authorization'] = token;
    Resp.Response response = await dio.get("/member/user/get");
    Log.d("获取用户信息结果： $response ");
    try {
      Map<String, dynamic> userInfoMap = jsonDecode(response.toString());
      UserInfoBean userInfoBean = UserInfoBean.fromJson(userInfoMap);

      if (userInfoBean.code == 0) {
        //信息获取成功
        updateName(userInfoBean.data?.nickname ?? "Lightthings");
        // updateAvatarUrl(userInfoBean.data?.avatar ?? "");
        updateData(0, userInfoBean.data?.sex ?? 0);
      } else {
        SmartDialog.showToast(userInfoBean.msg ?? "用户信息获取失败");
      }
    } catch (e) {
      print("e: $e");
    } finally {
      // SmartDialog.dismiss(status: SmartStatus.loading);
    }
  }

  commit() async {
    Log.d("提交个人信息");
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String token = prefs.getString(SP_ACCESS_TOKEN) ?? "";
    var dio = HttpUtil.instance?.serviceDio();
    dio.options.headers['tenant-id'] = "1";
    dio.options.headers['Authorization'] = token;
    Map<String, dynamic> data = {
      'nickname': userName.value,
      // 'avatar': avatarUrl.value,
      'sex': list.value[0].data,
    };
    Resp.Response response = await dio.put("/member/user/update", data: data);
    print("修改用户信息结果 ： $response");
    try {
      Map<String, dynamic> updateUserinfoMap = jsonDecode(response.toString());
      UpdateUserinfoBean updateUserinfoBean = UpdateUserinfoBean.fromJson(updateUserinfoMap);

      if (updateUserinfoBean.code == 0) {
        //密码修改成功
        Log.d("用户信息修改成功");
        Get.back();
      } else {
        SmartDialog.showToast(updateUserinfoBean.msg ?? "用户信息修改失败");
      }
    } catch (e) {
      print("e: $e");
    } finally {
      SmartDialog.dismiss(status: SmartStatus.loading);
    }
  }
}
